package froxy.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

public class Helper {
    public static String        PAC_FUNCTION_HEADER          = "function FindProxyForURL(url, host) {\nif (isPlainHostName(host))\nreturn \"%s\";\n";
    public static String        PAC_FUNCTION_CONTENT_PROXY   = "else if (shExpMatch(url, \"%s\"))\n return \"PROXY %s\";\n";
    public static String        PAC_FUNCTION_DIRECT          = "DIRECT";
    public static String        PAC_FUNCTION_CONTENT_DIRECT  = "else if (shExpMatch(url, \"%s\"))\n return \"DIRECT\";\n";
    public static String        PAC_FUNCTION_FOOTER          = "else\nreturn \"%s\";\n  }\n";
    public static AtomicInteger count                        = new AtomicInteger(0);
    public static ReentrantLock remoteLock                   = new ReentrantLock();
    public final static String  PREFIX_STATIC_CACHE          = "static";
    public final static int     BUFFER_SIZE                  = 4096;
    public final static String  sep                          = "/";
    public final static String  HTTP                         = "http";
    public final static String  HTTPPRO                      = "http://";
    public final static String  SEARCH                       = "archerfroxy";
    public final static String  SERVLETPATH                  = "/3/2/1/0/archerfroxy?";
    public final static String  NORMALPATH                   = "/3/2/1/0/archerfroxy";
    public final static String  FAQ                          = "/faq.html";
    public final static String  MAIN                         = "/index.html";
    public final static String  L0                           = "/3/2/1/0/";
    public final static String  L1                           = "/3/2/1/";
    public final static String  L2                           = "/3/2/";
    public final static String  L3                           = "/3/";
    public final static String  ENCRIPT_HEADER               = "FRANKJIAMI";
    public final static String  TRUE                         = "true";
    public final static String  FALSE                        = "false";
    public final static String  CLEAN_SESSION                = "/CLEANSESSION";
    public final static boolean BASE64                       = true;
    public final static String  GET                          = "GET";
    public final static String  POST                         = "POST";
    public final static String  EMPTYSTRING                  = "";
    public final static String  REFER_HEADER                 = "Referer";
    public final static String  Content_Length               = "Content-Length";
    public final static String  LOCATION_HEADER              = "Location";
    public final static String  Content_Encoding             = "Content-Encoding";
    public final static String  HTML_HEADER                  = "text/html";
    public final static String  CSS_HEADER                   = "text/css";
    public final static String  GZIP                         = "gzip";
    public final static String  UTF8                         = "UTF-8";
    public final static String  ISO88591                     = "ISO-8859-1";
    public final static String  GBK                          = "GBK";
    public final static String  BIG5                         = "BIG5";
    public final static String  GB2312                       = "GB2312";
    public final static String  HOST                         = "Host";
    public final static String  Set_Cookie                   = "Set-Cookie";
    public final static String  COOKIE                       = "Cookie";
    public final static String  COOKIE_PATH                  = "Path=";
    public final static String  COMMA                        = ",";
    public final static String  MYCLIENT_IN_SESSION          = "HTTPCLIENTINSESSION";
    public final static String  COOKIECONTEXT                = "ARCHEROXY_COOKIECONTEXT";
    public final static String  FROXYFRAMETAG                = "FROXYFRAMETAG";
    public final static String  DUMMYSEARCH                  = "dummyarcherfroxy";
    public final static String  APPSTATS                     = "/appstats";
    public final static String  SHARE                        = "/share";
    public final static String  HTML_PARSER                  = "HtmlParser";
    public final static String  JSOUP                        = "JSoup";
    public static String[]      REMOTE_SERVERS;
    public static int           REMOTE_SERVER                = 0;
    public static int           REMOTE_SERVER_FAIL           = 0;
    public static int           REMOTE_SERVER_FAIL_THRESHOLD = 10;
    public static boolean       ENCRIPT_ENABLEED             = true;
    private static boolean      useProxy                     = false;
    private static String       PACAddress                   = "";
    private static Properties   PROPERTIES                   = loadProperties();
    

    private Helper() {
    }

    public static Properties loadProperties() {
        String strPropertiesFilePath = null;
        FileInputStream fileInputStream = null;
        try {
            strPropertiesFilePath = "config.properties";
            PROPERTIES = new Properties();
            fileInputStream = new FileInputStream(new File(strPropertiesFilePath));
            PROPERTIES.load(fileInputStream);
            REMOTE_SERVERS = ((String) PROPERTIES.get("RemoteServer")).split(",");
            useProxy = Boolean.valueOf((String) PROPERTIES.get("UseProxy"));
            PACAddress = HTTPPRO + getLocalIP() + ":" + getFroxyPort() + "/pac";
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return PROPERTIES;
    }

    public static int getFroxyPort() {
        try {
            return Integer.valueOf((String) PROPERTIES.get("FroxyPort"));
        } catch (Exception e) {
            return 8888;
        }
    }

    public static String getRemoteServer() {
        return REMOTE_SERVERS[REMOTE_SERVER];
    }

    public static int getProxyPort() {
        try {
            return Integer.valueOf((String) PROPERTIES.get("ProxyPort"));
        } catch (Exception e) {
            return 80;
        }
    }

    public static int getQueuedThreadPool() {
        try {
            return Integer.valueOf((String) PROPERTIES.get("QueuedThreadPool"));
        } catch (Exception e) {
            return 40;
        }
    }

    public static int getMaxIdleTime() {
        try {
            return Integer.valueOf((String) PROPERTIES.get("MaxIdleTime"));
        } catch (Exception e) {
            return 30000;
        }
    }

    public static String getProxyServer() {
        return (String) PROPERTIES.get("LocalProxy");
    }

    public static String getPACAddress() {
        return PACAddress;
    }

    public static String getDefaultProxy() {
        return (String) PROPERTIES.get("DefaultProxy");
    }

    public static String getLocalIP() {
        return (String) PROPERTIES.get("FroxyIP");
    }

    public static String[] getProxyMask() {
        String proxyMask = (String) PROPERTIES.get("ProxyMask");
        if (proxyMask != null)
            return proxyMask.split(",");
        else
            return null;
    }

    public static String[] getDirectAccess() {
        String directAccess = (String) PROPERTIES.get("DirectAccess");
        if (directAccess != null)
            return directAccess.split(",");
        else
            return null;
    }

    public static String getDecryptClass() {
        if (PROPERTIES.get("DecryptClass") != null) {
            return (String) PROPERTIES.get("DecryptClass");
        } else {
            return null;
        }
    }

    public static boolean changeProxyServer() {
        int len = REMOTE_SERVERS.length;
        if (REMOTE_SERVER < (len - 1))
            REMOTE_SERVER++;
        else
            REMOTE_SERVER = 0;
        return true;
    }

    public static boolean useProxy() {
        return useProxy;
    }
}
